package com.shengwei.truck.driver.utils.glide;
import android.content.Context;
import android.os.Environment;
import androidx.annotation.NonNull;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.module.GlideModule;
import com.shengwei.truck.driver.utils.MyLogCat;

/**
 * 自定义Glide加载图片的磁盘缓存，图片质量等
 */
public class GlideConfiguration implements GlideModule {
    String TAG = "GlideConfiguration";
    String appCacheDirectory; //app安装包内部的缓存目录
    String sdCardDirectory = Environment.getExternalStorageDirectory().getPath();   //手机SD卡目录



    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //设置图片的显示格式ARGB_8888,如果怕图片太耗内存，可以改为DecodeFormat.PREFER_RGB_565
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        //自定义磁盘缓存
        appCacheDirectory = context.getCacheDir().getPath();
        int cacheSize100MegaBytes = 1024*1024*100; //缓存最大值100M
        builder.setDiskCache(new DiskLruCacheFactory( getStorageDirectory(),cacheSize100MegaBytes));
    }

    /**
     * 选取一个缓存目录，如果挂载了SD卡，则返回SD卡下的目录，否则返回APP安装包内部的目录
     * @return
     */
    public String getStorageDirectory() {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String sdDirctory =  sdCardDirectory + "/ATruckDriver";
            MyLogCat.i(TAG,"sdDirctory="+sdDirctory);
            return sdDirctory;
        }
       return appCacheDirectory;
    }


    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
     /* 用于图片进度加载  OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new ProgressInterceptor())
                .build();

        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));*/
    }
}